package cn.gwm.flink.streaming.strategy.vehiclepredict;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author : GW00280745
 * @date : 2022/10/10 9:02
 * @description : 每单位soc预测时，如果map表中的值不存在，那么调用这个类
 */
public class SocMapDefault implements Serializable {
    private JSONObject defaultDcBatterMap;
    public SocMapDefault(JSONObject defaultDcBatterMap){
        //等价于 default-dc-batter-map.properties
        this.defaultDcBatterMap =defaultDcBatterMap;
    }

    public BigDecimal batterDefault(String soc, String temp, String voltMin, String voltMax){
        String key = keyBind(soc, temp, voltMin, voltMax);
        BigDecimal value = defaultDcBatterMap.getBigDecimal(key);
        return value;
    }

    public static void main(String[] args) {
        SocMapDefault socMapDefault = new SocMapDefault(null);
        String s = socMapDefault.keyBind("95.7", "29.54", "3.421", "3.432");
        System.out.println();
    }
    /**
     * @description:  default-dc-batter-map.properties中的key进行拼接
     *  类似 soc<<5&vmin<2.4&vmin>>1.8&temp>>30&temp<35
     *      soc<<5&vmin>>2.4&temp<-20
     * @author:  GW00280745
     * @date: 2022/10/10 9:09
     * @param:
     * @return:
 * @return: java.lang.String
     **/
    public String keyBind(String soc,String temp,String voltMin,String voltMax){
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //1.soc坐标   (0,5]&vmin[1.8,2.4)&temp[15,20)
            float socF = Float.parseFloat(soc);
            String soxAxis;
            if (socF<=DwsStrConstant.ins().num10){
                soxAxis = axis(soc, 0, 10, 5, "(", "]");
            }else if (socF>=DwsStrConstant.ins().num80){
                soxAxis = axis(soc, 80, 100, 5, "(", "]");
            }else {
                soxAxis = axis(soc, 10, 90, 10, "(", "]");
            }
            //2.温度坐标
            String tempAxis = axis(temp, -20, 55, 5, "[", ")");
            stringBuilder.append("soc").append(soxAxis).append(separator).append("temp").append(tempAxis);
            //3. 电压坐标
            if (socF<=DwsStrConstant.ins().num5){
                String voltMinAxis = axisFloat(voltMin, 1.8f, 2.4f, 0.6f, "[", ")");
                stringBuilder.append(separator).append("vmin").append(voltMinAxis);
            }else if (socF >DwsStrConstant.ins().num95){
                String voltMaxAxis = voltMaxAxisStr(voltMax);
                stringBuilder.append(separator).append("vmax").append(voltMaxAxis);
            }
            return stringBuilder.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private String separator="&";
    private float f360 =3.60f;
    private float f365 =3.65f;
    private float f358 =3.58f;
    private float f355 =3.55f;
    private float f352 =3.52f;
    private String voltMaxAxisStr(String voltMax){
        try {
            float voltMaxF = Float.parseFloat(voltMax);
            if (voltMaxF>=f360 && voltMaxF <= f365){
                return "[3.60,3.65]";
            }else if (voltMaxF >= f358 && voltMaxF <= f365){
                return "[3.58,3.65]";
            }else if (voltMaxF >= f355 && voltMaxF <= f365){
                return "[3.55,3.65]";
            }else if (voltMaxF >= f352 && voltMaxF <= f365){
                return "[3.52,3.65]";
            }else {
                return "N";
            }
        }catch (Exception e){
            return "N";
        }
    }
    private String blankStr = "";
    private String comma = ",";
    private String greatEq = ">>";
    private String less = "<";
    /**
     * @description: 求某个值所在范围 例如 (21,-25,30,5)  => [20,25)
     * @author:  GW00280745
     * @date: 2022/7/23 15:33
     * @param: value 当前值
     * @param: start 开头
     * @param: end   结尾
     * @param: step  步长
     * @return: java.lang.String  [20,25)
     **/
    public String axis(String value, int start, int end, int step,String leftMidparentheses,String rightParentheses) {
        if (StringUtils.isBlank(value)){
            return blankStr;
        }
        try {
            Double.parseDouble(value);
        }catch (NumberFormatException nfe){
            nfe.printStackTrace();
            return value;
        }
        double v = Double.parseDouble(value);
        if (v>=end){
            return  greatEq.concat(String.valueOf(end));
        }else if (v < start){
            return less.concat(String.valueOf(start));
        }
        for (int i = start; i < end; i=i+step) {
            if (v >= i && v < i+step){
                return leftMidparentheses.concat(String.valueOf(i))
                        .concat(comma)
                        .concat(String.valueOf(i+step))
                        .concat(rightParentheses)
                        ;
            }
        }
        return blankStr;
    }

    public String axisFloat(String value, float start, float end, float step,String leftMidparentheses,String rightParentheses) {
        if (StringUtils.isBlank(value)){
            return blankStr;
        }
        try {
            Double.parseDouble(value);
        }catch (NumberFormatException nfe){
            nfe.printStackTrace();
            return value;
        }
        double v = Double.parseDouble(value);
        if (v>=end){
            return  greatEq.concat(String.valueOf(end));
        }else if (v < start){
            return less.concat(String.valueOf(start));
        }
        for (float i = start; i < end; i=i+step) {
            if (v >= i && v < i+step){
                return leftMidparentheses.concat(String.valueOf(i))
                        .concat(comma)
                        .concat(String.valueOf(i+step))
                        .concat(rightParentheses)
                        ;
            }
        }
        return blankStr;
    }
}
